﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Configuration;
using NLog;

namespace ABC.Database
{
    [Serializable]
    public class DatabaseConnectionException : Exception
    {
        public DatabaseConnectionException() { }
        public DatabaseConnectionException(string message) : base(message) { }
        public DatabaseConnectionException(string message, Exception inner) : base(message, inner) { }
        protected DatabaseConnectionException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    public static class DataBaseConnection
    {
        private static Logger logger = LogManager.GetLogger("DataBaseConnection");
        public static OleDbConnection MyConn
        {
            get
            {
                logger.Info("Class: DataBaseConnection: get MyConn: ");
                LogManager.Flush();

                if (conn == null)
                {
                    conn = new OleDbConnection();
                    //conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" +
                    //         "SERVER=localhost;" +
                    //         "DATABASE=sakila;" +
                    //         "UID=root;" +
                    //         "PASSWORD=;";
                    //string schema = OledbBridge.dbSchemaProp;
                    //if (schema.ToLower() == "abc")
                    //    schema = "abc-run.mdb";
                    //else
                    //    schema = "abc-rundev.mdb";

                    //REMOVE when using for web site. 
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
                    
                    //conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\abc-run.mdb;Jet OLEDB:System Database=b:\\ABCElectronicis\\SERVICE.mdw;User ID=programmer;Password=Brokeit;";

                    //PROD
                    //conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\abcd\service-data\service\abc-run.mdb;Jet OLEDB:System Database=\\abcd\service-data\SERVICE.MDW;User ID=programmer;Password=Brokeit;";//ConfigurationManager.AppSettings["dbconnection"];
                   
                  
                 
                    try
                    {
                       conn.Open();
                   
                        if (conn.State == System.Data.ConnectionState.Closed)
                            throw new Exception();
                    }
                    catch(Exception ex)
                    {
                        throw new DatabaseConnectionException(ex.Message, ex);
                    }

                }
                return conn;
            }
        }
        private static OleDbConnection conn;
    }
}
